package com.kongwg.transaction;

import com.kongwg.util.JDBCUtil;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.*;

/**
 * @author kongwg
 * @create 2021-01-08 17:13
 */
public class TransactionTest {
    /**
     * 添加事务之后操作数据进行转账
     */

    @Test
    public void testUpdate() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            String sql = "update user_table set balance = balance - 500 where `user` = ?";
            update(conn, sql, "AA");

            String sql1 = "update user_table set balance = balance + 500 where `user` = ?";
            update(conn, sql1, "BB");

            System.out.println("转账成功");
            conn.commit();
        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        } finally {

            JDBCUtil.closeResource(conn, null);
        }


    }

    @Test
    public void testTransactionSelect() throws Exception {
        Connection conn = JDBCUtil.getConnection();
        System.out.println(conn.getTransactionIsolation());
        conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
        String sql = "select user as name,password,balance from user_table where user = ?";
        User user = query(conn, User.class, sql, "CC");
        System.out.println(user);

    }

    @Test
    public void testTransactionUpdate() throws Exception {
        Connection conn = JDBCUtil.getConnection();
        String sql1 = "update user_table set balance = ? where user = ?";
        update(conn,sql1,"4000" , "CC");
        Thread.sleep(1500);
    }

    public void update(Connection conn, String sql, Object... args) throws Exception {

        PreparedStatement ps = conn.prepareStatement(sql);
        for (int i = 0; i < args.length; i++) {
            ps.setObject(i + 1, args[i]);
        }
        ps.execute();

        JDBCUtil.closeResource(null, ps);

    }


    /**
     * 考虑事务查询一条数据
     */
    public <T> T query(Connection conn, Class<T> clazz, String sql, Object... args) throws Exception {
        PreparedStatement ps = conn.prepareStatement(sql);

        for (int i = 0; i < args.length; i++) {
            ps.setObject(i + 1, args[i]);

        }

        ResultSet rs = ps.executeQuery();
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        if (rs.next()) {
            T t = clazz.newInstance();
            for (int i = 0; i < columnCount; i++) {
                Object columnValue = rs.getObject(i + 1);

                String columnLabel = rsmd.getColumnLabel(i + 1);

                Field field = t.getClass().getDeclaredField(columnLabel);
                field.setAccessible(true);
                field.set(t, columnValue);
            }

            return t;
        }
        JDBCUtil.closeResource(null, ps, rs);
        return null;
    }


}
